﻿using Microsoft.Extensions.Caching.Memory;

namespace YDT.Projects.WebSite.CacheHttpClient
{
    public class RequeHttpClientMessageHanlder : DelegatingHandler
    {
        private readonly IMemoryCache _memoryCache;// 本地缓存

        public RequeHttpClientMessageHanlder(IMemoryCache memoryCache) 
        {
            _memoryCache = memoryCache;
        }

        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            // 2、本地缓存
            HttpResponseMessage httpResponseMessage = _memoryCache.Get<HttpResponseMessage>(request.RequestUri.AbsoluteUri);
            httpResponseMessage = new HttpResponseMessage();
            httpResponseMessage.StatusCode = System.Net.HttpStatusCode.Accepted;
            httpResponseMessage = base.SendAsync(request, cancellationToken).Result;
            return httpResponseMessage;
        }
    }
}
